# -*- coding: utf-8 -*-            
# @Time : 2022/10/31 23:26
# @Author  : lining
# @FileName: zuoye2.py
"""
将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
l1 = [1,2,4], l2 = [1,3,4]
输出：[1,1,2,3,4,4]
"""
# Definition for singly-linked list.
class ListNode(object):
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


class Solution(object):
    def mergeTwoLists(self, list1, list2):
        """
        :type list1: Optional[ListNode]
        :type list2: Optional[ListNode]
        :rtype: Optional[ListNode]
        """
        # 创建一个保护节点
        head = ListNode()
        # 当前已经拼接的最后一个节点
        current = head
        while True:
            if list1 is None and list2 is None:
                break
            if list1 is None or list2 is None:
                current.next = list2 if list2 else list1
                break
            elif list1.val <= list2.val:
                current.next = list1
                list1 = list1.next
            elif list1.val > list2.val:
                current.next = list2
                list2 = list2.next
            current = current.next
        return head.next


a1 = ListNode(3)
a2 = ListNode(2, a1)
a3 = ListNode(1, a2)
b1 = ListNode(6)
b2 = ListNode(5, b1)
b3 = ListNode(4, b2)

c = Solution().mergeTwoLists(a3, a3)
print(111)


